<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'main';

if ( $f == 'main' ) {
	
	$sql = "SELECT a.username, a.sex, a.signature, a.email, a.msn, a.qq, a.regtime, a.lastlogin, a.logincount, a.ip, b.groupname FROM ".DB_PREFIX."users a JOIN ".DB_PREFIX."usergroups b ON a.groupid = b.gid  WHERE a.uid = '".$_G['uid']."'";
	$userinfo = $_G['db']->fetch_one_array($sql);

} elseif ( $f == 'profile' ) {
	
	if ( check_submit('dosubmit') ) {
		$password=getGP('password');
		$newpassword=getGP('newpassword');
		$newpassword2=getGP('newpassword2');
		$sex=getGP('sex');
		$signature=getGP('signature');
		$qq=getGP('qq');
		$msn=getGP('msn');
		$data = array();

		if ( empty($password) ) {
			msg('请输入你的密码！');
		}
		if ( !$_G['db']->result("SELECT id FROM ".DB_PREFIX."user WHERE uid = '".$_G['uid']."' AND password = '".md5($password)."'") ) {
			msg('你输入的密码有误！');
		}
		if ( !empty($newpassword) ) {
			if ( strlen($newpassword) < 6 ) {
				msg('密码长度不能小于6位！');
			} elseif ( $newpassword != $newpassword2 ) {
				msg('两次输入的密码不一致！');
			} else {
				$data['password'] = md5($newpassword);
			}
		}
		if ( in_array($sex, array(0,1,2)) ) $data['sex'] = $sex;
		if ( preg_match('/^http:\/\/[0-9a-z\-\.]{4,50}\.[a-z]{2,5}$/i', $signature)) $data['signature'] = $signature;
		if ( preg_match('/^[1-9][0-9]{4,15}$/', $qq)) $data['qq'] = $qq;
		if ( is_email($msn)) $data['msn'] = $msn;
		$_G['db']->update('user', $data, array('uid' => $_G['uid']));
		msg('资料修改成功。');
		
	} else {

		$sql = "SELECT username,sex,signature,email,msn,qq FROM ".DB_PREFIX."users WHERE uid = '".$_G['uid']."'";
		$userinfo = $_G['db']->fetch_one_array($sql);

	}
	
} elseif ( $f == 'comment' ) {

	$page = max(1, getGP('page','G','int'));
	$pagesize = 10;
	$offset = ($page-1) * $pagesize;
	$url = '?m=ucenter&f=comment';
	$wheresql = " a.uid = '".$_G['uid']."' ";
	$num = $_G['db']->result("SELECT COUNT(*) FROM ".DB_PREFIX."comment a JOIN ".DB_PREFIX."article b ON a.referid = b.aid WHERE $wheresql");
	$result = $_G['db']->fetch_all("SELECT a.*, b.title FROM ".DB_PREFIX."comment a JOIN ".DB_PREFIX."article b ON a.referid = b.aid WHERE $wheresql ORDER BY a.cid DESC LIMIT $offset, $pagesize");

} 

include(ADMIN_ROOT.'./template/ucenter.tpl.php');
?>